package com.example.springdemo.enums;

import com.baomidou.mybatisplus.annotation.IEnum;
import com.example.springdemo.annotation.EnumConvertMethod;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Status
 * 状态 1：正常，0：删除
 *
 * @author wenguang
 * @date 2020/12/11 15:21
 */
public enum Status implements IEnum<Integer> {

    /**
     * 无效
     */
    INVALID(0, "无效"),

    /**
     * 有效
     */
    VALID(1, "有效");

    /**
     * 唯一索引值
     */
    @JsonValue
    public final int value;

    /**
     * 描述
     */
    //@JsonValue
    public final String desc;

    Status(int value, String desc) {
        this.value = value;
        this.desc = desc;
    }

    @Override
    public Integer getValue() {
        return value;
    }

    private static final Map<Integer, Status> STATUS_MAP;

    static {
        Map<Integer, Status> temp = Arrays.stream(values())
            .collect(Collectors.toMap(Status::getValue, Function.identity()));
        STATUS_MAP = Collections.unmodifiableMap(temp);
    }

    @EnumConvertMethod
    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static Status resolve(int index) {
        return STATUS_MAP.get(index);
    }

    public static Optional<Status> getByValue(int value) {
        return Optional.ofNullable(STATUS_MAP.get(value));
    }
}
